
$(function() {
	Tip.emp('借款人');
	
	$('#emp').blur(function() {
		var a = $(this).val();
		Pay.emp(a);
	});
	
	Pay.emp($('#emp').val());
		
	_ck_default("input[id='emp']:visible", "input[id^='emp_t']:visible");
	
	$('#currency').change(function() {
		var a = $("#amount").val();
		var b = $(this).find("option:selected").text();
	
		Currency.upper(a, b, "#label_amount");
	});
	
	$('#amount').keyup(function() {
		var a = $(this).val();
		var b = $("#currency").find("option:selected").text();
		Currency.upper(a, b, "#label_amount");
	});
	
	$('#date1').datepicker(_date1_option);
	$('#date2').datepicker(_date1_option);
	
	$('#date1').change(function() {
		_cal();
	});
	
	$('#date2').change(function() {
		_cal();
	});
	
	$('#day_t').change(function() {
		var s = $(this).val();
		if (s != null && s != '-')　{
			$('#day').val(s);
		}
		
		_day();
	});
	
	$('#day').change(function() {
		_day();
	});
	
	$('#payment').change(function() {
		var a = $(this).val();
		Pay.payment(a);
	});
	
	Pay.payment($('#payment').val());
	
	$("#label_payment_bank").hide();
	
	$('textarea').css("width", 400);
});

function _currency_upper(s, b) {
	s = s.toString();	
	if (s == "") {
		return "";
	}
	
	if (s.match(/[^,.\d]/) != null) {
		return "";
	}
	
	if (s.match(/^((\d{1,3}(,\d{3})*(.((\d{3},)*\d{1,3}))?)|(\d+(.\d+)?))$/) == null) {
		return "";
	}
		
	var r = Currency.upper(s);
	
	var s = $("#label_amount");		
	s.text("（大写）" + b + " " + r);
}

function _day() {
	var t1 = $('#date1').val();
	var dd = $('#day').val();
		
	t1 = t1.replace(/\-/g, "/");
	
	var date = new Date(t1);
	date.setDate(date.getDate() + parseInt(dd));
	
	var dt = $.dateFormat.date(date, "yyyy-MM-dd");
	var t1 = $('#date2').val(dt);
    // alert(dt);
	
	_cal();
}

function _cal() {
	var t1 = $('#date1').val();
	// alert(t1);
	if (t1 == undefined || t1 == '') {
		return;
	}
	
	var t2 = $('#date2').val();
	// alert(t2);
	if (t2 == undefined || t2 == '') {
		return;
	}
	
	var day = GetDateDiff(t1, t2, "day");
	if (day <= 0) {
		return ;
	}
	
	$('#day').val(day);
}

